<template>
  <div class="page-container padding-20 query-list-container">
    <div v-show="$route.name === 'channelMgt'">
      <div class="search-container">
        <el-form :inline="true" ref="form" :model="searchData" size="small">
          <el-form-item label="关键字输入">
            <el-input v-model="searchData.name" placeholder="渠道商名称"></el-input>
          </el-form-item>
          <el-form-item label="权益产品">
            <el-select v-model="searchData.productId" clearable>
              <el-option
                v-for="product in productList"
                :key="product.id"
                :label="product.name"
                :value="product.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所在地区">
            <area-select v-model="region"
              :emitPath="true"></area-select>
          </el-form-item>
          <el-button type="primary" @click="onSearch" size="small">查询</el-button>
          <el-button @click="onReset" size="small">重置</el-button>
        </el-form>
      </div>
      <div class="toolbar-container">
        <el-button type="primary" icon="el-icon-plus" size="small" @click="linkToAdd">新建</el-button>
      </div>
      <el-table class="mt-20 el-table-modify" :data="tableData" border>
        <!-- <el-table-column type="index" label="序号" align="center" width="50"></el-table-column> -->
        <el-table-column prop="id" align="center" label="渠道商ID"></el-table-column>
        <el-table-column prop="name" align="center" label="渠道商"></el-table-column>
        <el-table-column prop="regionName" align="center" label="所在地区"></el-table-column>
        <el-table-column prop="saleSource" align="center" label="销售来源"></el-table-column>
        <el-table-column prop="productName" align="center" label="权益产品"></el-table-column>
        <el-table-column prop="brandName" align="center" label="权益品牌"></el-table-column>
        <el-table-column prop="saleAmount" align="center" label="已销售数量"></el-table-column>
        <el-table-column prop="createdAt" align="center" label="创建时间">
          <template slot-scope="scope">
            {{scope.row.createdAt | dateFrm}}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="130">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="linkToEdit(scope.row)">修改</el-button>
            <el-button size="mini" type="text" @click="linkToDetail(scope.row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        ref="pagination"
        class="mt-20"
        background
        :page-size.sync="pageOption.pageSize"
        :total="pageOption.total"
        @size-change="onSizeChange"
        @current-change="onPageChange"
        layout="slot,->,prev,pager,next,sizes,jumper"
      >
        <span
          class="pagination-title"
        >共 {{pageOption.total}} 条记录 第 {{pageOption.page}} / {{pageOption.pageTotal}} 页</span>
      </el-pagination>
    </div>
    <router-view v-if="$route.name !== 'channelMgt'" @refresh="this.requestData"></router-view>
  </div>
</template>

<script>
import {channelQuery} from '@/api/channel';
import {productEnableAll} from '@/api/product';
import AreaSelect from '@/components/AreaSelect';

export default {
  name: "ChannelMgt",
  components: {AreaSelect},
  data() {
    return {
      productList: [],
      region: [],
      searchData: {
        name: "",
        productId: "",
        area: ""
      },
      tableData: [],
      pageOption: {
        page: 1,
        pageSize: 20,
        total: 0,
        pageTotal: 1
      },
      selectData: null,
      visible: false
    };
  },
  methods: {
    areaChange(area) {
      this.searchData.area = area;
    },
    onSearch() {
      this.pageOption.page = 1;
      this.requestData();
    },
    onReset() {
      this.region = [];
      this.searchData.name = '';
      this.searchData.productId = '';
      this.pageOption.area = "";
      this.pageOption.page = 1
      this.requestData();
    },
    linkToAdd() {
      this.$router.push({name: 'addChannel'})
    },
    linkToEdit(row) {
      this.$router.push({name: 'editChannel', params: {id: row.id}})
    },
    linkToDetail(row) {
      this.$router.push({name: 'channelDetail', params: {id: row.id}});
    },
    async requestData() {
      const {page, pageSize : size} = this.pageOption   
      const postData = {}, searchData = this.searchData;
      Object.keys(searchData).forEach(item => {
        if (searchData[item]) {
          postData[item] = searchData[item];
        }
      })
      if (searchData.area && searchData.area.length > 0) {
        postData[area] = searchData.area.join(',')
      }

      const {data, total} = await channelQuery({page: page - 1, size, ...postData});
      this.tableData = data;
      this.pageOption.total = total;
      this.pageOption.pageTotal = total ? Math.ceil(total / size) : 1

      let childrenRefs = this.$refs.pagination.$children
      this.$nextTick(() => {
        childrenRefs.forEach(child => child.$forceUpdate())
      });
    },
    async requestProduct() {
      try {
        const {data} = await productEnableAll();
        this.productList = data;
      } catch(err) {
        console.log(err)
      }
    },
    onSizeChange(size) {
      this.pageOption.pageSize = size;
      this.requestData();
    },
    onPageChange(page) {
      this.pageOption.page = page;
      this.requestData();
    }
  },
  created() {
    this.requestData();
    this.requestProduct();
  }
};
</script>

<style lang="scss">
</style>